'use strict';

/**
 * City Profile Controller
 */
app.controller('AreaCtrl',
    ['$scope', '$http', 'dialogs', '$state', 'AreaService',
        function ($scope, $http, dialogs, $state, AreaService) {
            var vm = $scope.vm = this;
            var defaultQuery = {};
            vm.query = angular.copy(defaultQuery);

            vm.flexigridObjDModel = {};
            vm.options = AreaService.getGirdOptions();

            vm.treeObj = {};
            vm.treeSetting = {
                async: {
                    url: ISS.getPath('/arch/tree'),
                    enable: true,
                    dataType: "json",
                    autoParam: ["id"]
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: function (event, treeId, treeNode, clickFlag) {
                        setParam(treeNode);
                    },
                    onAsyncSuccess: function (event, treeId, treeNode, msg) {
                        //默认展开根节点，触发根节点展开点击事件
                        if (!treeNode) {
                            $("#" + treeId + "_1_switch").click();
                        } else {
                            /*vm.rootNode = treeNode;
                             if (vm.node) {
                             for (var item in treeNode.children) {
                             if (treeNode.children[item].id == vm.node.id) {
                             $("#" + treeNode.children[item].tId + "_span").click();
                             break;
                             }
                             }
                             } else {
                             treeNode.children.length && $("#" + treeNode.children[0].tId + "_span").click();
                             }*/
                        }
                    }
                }
            };

            // 重新异步加载子节点，新增或修改的时候
            var reAsyncChildNodes = function () {
                vm.treeObj.reAsyncChildNodes(null, "refresh");
            }
            // 设置查询参数
            var setParam = function (data) {
                vm.selectedId = data.id;
                vm.selectedName = data.name;
                // 点击建筑树节点时，查询该建筑节点下一级的建筑（不包括该节点）
                vm.flexigridObjDModel.flexOptions({
                    newp: 1,
                    extParam: {'parentId': data.id}
                }).flexReload();
            }
            //查询
            vm.search = function () {
                var param = angular.copy(vm.query);
                /* param[param.searchType] = param;
                 delete param.searchType;
                 delete param.keyword;*/
                vm.flexigridObjDModel.flexOptions({
                    newp: 1,
                    extParam: param
                }).flexReload();
            }

            // 重置
            vm.reset = function () {
                vm.treeObj.cancelSelectedNode();
                vm.selectedId = null;
                vm.selectedName = null;
                vm.query = angular.copy(defaultQuery);
                vm.search();
            }
            // 新增
            vm.addArea = function () {
                if (!ISS.isEmpty(vm.selectedId)) {
                    var area = {
                        parentType: vm.selectedType,
                        modeId: vm.areaModeId,
                        parentId: vm.selectedId,
                        parentName: vm.selectedName,
                        leaf: ''
                    }
                    var opData = {area: area};
                    AreaService.opData = opData;
                } else {
                    AreaService.opData = null;
                }

                $state.go('setting.area.add');
            }
            // 修改
            vm.updateArea = function (id) {
                // 如果只选择一条记录，获取该记录的信息并显示修改页面
                if (ISS.isNotEmpty(id)) {
                    $state.go('setting.area.update', {id: id});
                } else {
                    dialogs.notify(undefined, getMessage("common.plzSelectOneRecourd"));
                }
            }
            // 删除
            vm.delArea = function (id) {
                if (ISS.isNotEmpty(id)) {
                    // 弹出确认删除提示信息
                    var dlg = dialogs.confirm(undefined, getMessage("common.deleteConfirm"));
                    dlg.result.then(function (param) {
                        AreaService.deleteArea(id).then(function (data) {
                            // 刷新列表
                            vm.flexigridObjDModel.flexReload();
                            // 刷新左侧树
                            reAsyncChildNodes();
                            //弹出提示信息
                            dialogs.notify(data.msg);
                        });
                    });
                } else {
                    // 提示请选择一条或多条信息
                    dialogs.notify(undefined, getMessage("common.plzSelectOneRecourd"));
                }
            }

        }]);


app.controller('CityProfileModelModifyCtrl', ['$scope', '$state', 'dialogs', 'AreaService',
    function ($scope, $state, dialogs, AreaService) {
        var vm = $scope.vm = this;

        vm.title = "common.add";
        vm.editFlag = false;

        var data = AreaService.opData;
        if (data) {
            vm.area = data.area;
        } else {
            vm.area = {parentId: 0, level: 1};
        }

        function cancel() {
            $state.go('setting.area.list');
        }

        if (!ISS.isEmpty($state.params.id)) {
            //修改
            vm.editFlag = true;
            vm.area.id = $state.params.id;
            vm.title = "common.edit";
            //查询要修改的建筑
            AreaService.getArea(vm.area.id).then(function (data) {
                if (data.success === false) {
                    cancel();
                } else {
                    vm.area = data;
                }
            }, function () {
                cancel();
            });
        } else {
            AreaService.getNextNumber().then(function (data) {
                if (data.success === false) {
                } else {
                    vm.area.number = data.number;
                }
            });
        }

        vm.cancel = function () {
            cancel();
        };
        // 提交数据
        vm.save = function () {

            if (vm.editFlag) {
                AreaService.updateArea(angular.copy(vm.area)).then(function (data) {
                    if (data.success === false) {
                        dialogs.notify(data);
                    } else {
                        dialogs.notify(data).closed.then(function () {
                            cancel();
                        });
                    }
                });
            } else {
                AreaService.addArea(angular.copy(vm.area)).then(function (data) {
                    if (data.success === false) {
                        dialogs.notify(data);
                    } else {
                        dialogs.notify(data).closed.then(function () {
                            cancel();
                        });
                    }
                });
            }
        };

        vm.location = function (lngLat) {
            if (lngLat) {
                vm.area.longitude = lngLat.lng;
                vm.area.latitude = lngLat.lat;
            }
        }
    }]);
